package com.cote.douhahou.pretloan.msg;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.beans.BaAkomapalendnkAccount;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class SubmitAccountAkomapalendDialog extends Dialog {

    private LinearLayout groupLayout_akomapalend;

    private final Runnable submitRunnable_akomapalend;

    private final String page_akomapalend;

    public SubmitAccountAkomapalendDialog(@NonNull Context context, Runnable submitRunnable, String page) {
        super(context);
        this.submitRunnable_akomapalend = submitRunnable;
        this.page_akomapalend = page;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_submit_account_list);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        findViewById(R.id.dsafa_view_out).setOnClickListener(v -> dismiss());
        findViewById(R.id.dsafa_view_in).setOnTouchListener((v, event) -> true);

        initView_akomapalend();
    }

    @SuppressLint("SetTextI18n")
    private void initView_akomapalend(){

        groupLayout_akomapalend = findViewById(R.id.dsafa_layout_accounts_group);

        findViewById(R.id.dsafa_btn_submit).setOnClickListener(v -> {
            HttpHelper.inst().submitEvent(page_akomapalend,"SUBMIT_CONFIRM_CLICK","");
            if (submitRunnable_akomapalend !=null) submitRunnable_akomapalend.run();
            dismiss();
        });

        refreshAccountList_akomapalend();

    }

    private void refreshAccountList_akomapalend(){
        groupLayout_akomapalend.removeAllViews();

        ArrayList<BaAkomapalendnkAccount> accounts = AccAkomapalendountList.instance().getBankAccountList_akomapalend();

        for (BaAkomapalendnkAccount account:accounts){
            addAccountView_akomapalend(account);
        }

    }

    private void addAccountView_akomapalend(BaAkomapalendnkAccount account){

        if (account == null) return;

        String desc = account.getWalletDesc_akomapalend();

        View showAccountView = View.inflate(getContext(), R.layout.item_show_account, null);
        ImageView logoIv = showAccountView.findViewById(R.id.isafa_iv_logo);
        TextView channelTv = showAccountView.findViewById(R.id.isafa_tv_channel);
        TextView mobileTv = showAccountView.findViewById(R.id.isafa_tv_mobile);

        mobileTv.setText(account.getWalletMobile_akomapalend());
        channelTv.setText(desc);

        groupLayout_akomapalend.addView(showAccountView);
        
        if (AppAkomapalendConfigCustomize.instance().getChannelLogoByDesc_akomapalend(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_akomapalend())) return;
                    if (!AppAkomapalendConfigCustomize.instance().hasAccountChannel_akomapalend()) return;
                    URL url = new URL(account.getWalletLogo_akomapalend());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        AppAkomapalendConfigCustomize.instance().setChannelLogoByDesc_akomapalend(desc,bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(AppAkomapalendConfigCustomize.instance().getChannelLogoByDesc_akomapalend(desc));

    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpHelper.inst().submitEvent(page_akomapalend,"SUBMIT_CANCEL_CLICK","");
    }

}
